SpringBoot接口实现视频在线播放

您所在的位置:网站首页 sprain ankle8视频 SpringBoot接口实现视频在线播放

SpringBoot接口实现视频在线播放

2023-06-01 22:21| 来源: 网络整理| 查看: 265

 获取所有的mp4在线播放地址,并当前端调用该接口时返回mp4视频文件名及其播放地址,mp4视频存放在D盘的video文件夹下

@ApiOperation("获取所有MP4播放地址") @GetMapping("/get/video/address") public List getVideoAddress() { File file = new File("D:\\video"); List fileNs = new ArrayList(); String filename = ""; File[] subFile = file.listFiles(); for (File value : subFile) { // 判断是否为文件夹 if (!value.isDirectory()) { Map jsonVideos = new HashMap(); String subFileName = value.getName(); // 判断是否为mp4结尾 if (subFileName.trim().toLowerCase().endsWith(".mp4")) { filename = "http://localhost:8080/video/alone/video/play" + subFileName; String chFileName = VIDEO_EN_CH.get(subFileName); jsonVideos.put("filename",chFileName); jsonVideos.put("address",filename); fileNs.add(jsonVideos); } } } return fileNs; }

单个视频在线播放接口,这里需要传mp4文件名

@ApiOperation("单个MP4播放") @GetMapping(value = "/alone/video/play/{filename}" ,produces ="application/json;charset=utf-8") public void aloneVideoPlay(HttpServletRequest request, @PathVariable("filename") String fileName, HttpServletResponse response) { InputStream is = null; OutputStream os = null; try { response.setContentType("video/mp4"); File file = new File("D:\\video\\" + fileName); response.addHeader("Content-Length", "" + file.length()); is = new FileInputStream(file); os = response.getOutputStream(); IOUtils.copy(is, os); } catch (Exception e) { log.error("播放MP4失败", e); } finally { if (null != os) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } }

完整Controller

@Api(tags = "视频Controller") @RestController @RequestMapping("/video") public class VideoController { @ApiOperation("获取所有MP4播放地址") @GetMapping("/get/video/address") public List getVideoAddress() { File file = new File("D:\\video"); List fileNs = new ArrayList(); String filename = ""; File[] subFile = file.listFiles(); for (File value : subFile) { // 判断是否为文件夹 if (!value.isDirectory()) { Map jsonVideos = new HashMap(); String subFileName = value.getName(); // 判断是否为mp4结尾 if (subFileName.trim().toLowerCase().endsWith(".mp4")) { filename = "http://localhost:8080/video/alone/video/play" + subFileName; String chFileName = VIDEO_EN_CH.get(subFileName); jsonVideos.put("filename",chFileName); jsonVideos.put("address",filename); fileNs.add(jsonVideos); } } } return fileNs; } @ApiOperation("单个MP4播放") @GetMapping(value = "/alone/video/play/{filename}" ,produces ="application/json;charset=utf-8") public void aloneVideoPlay(HttpServletRequest request, @PathVariable("filename") String fileName, HttpServletResponse response) { InputStream is = null; OutputStream os = null; try { response.setContentType("video/mp4"); File file = new File("D:\\video\\" + fileName); response.addHeader("Content-Length", "" + file.length()); is = new FileInputStream(file); os = response.getOutputStream(); IOUtils.copy(is, os); } catch (Exception e) { log.error("播放MP4失败", e); } finally { if (null != os) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } } }

扩展对视频进行分页

        PageUtil

public class PageUtil { /** * 开始分页 * @param list * @param pageNum 页码 * @param pageSize 每页多少条数据 * @return */ public static List startPage(List list, Integer pageNum, Integer pageSize) { if (list == null) { return null; } if (list.size() == 0) { return null; } Integer count = list.size(); // 记录总数 Integer pageCount = 0; // 页数 if (count % pageSize == 0) { pageCount = count / pageSize; } else { pageCount = count / pageSize + 1; } int fromIndex = 0; // 开始索引 int toIndex = 0; // 结束索引 if (pageNum != pageCount) { fromIndex = (pageNum - 1) * pageSize; toIndex = fromIndex + pageSize; } else { fromIndex = (pageNum - 1) * pageSize; toIndex = count; } List pageList = list.subList(fromIndex, toIndex); return pageList; } }

获取所有MP4播放地址(分页),需要前端调该接口时,传两个参数,pageNum及pageSize,页码和每页多少条

@ApiOperation("获取所有MP4播放地址") @PostMapping("/get/video/address") public List getVideoAddress(@RequestBody Map json) { File file = new File("D:\\video"); List fileNs = new ArrayList(); String filename = ""; File[] subFile = file.listFiles(); for (File value : subFile) { // 判断是否为文件夹 if (!value.isDirectory()) { Map jsonVideos = new HashMap(); String subFileName = value.getName(); // 判断是否为mp4结尾 if (subFileName.trim().toLowerCase().endsWith(".mp4")) { filename = "http://localhost:8080/video/alone/video/play" + subFileName; String chFileName = VIDEO_EN_CH.get(subFileName); jsonVideos.put("filename",chFileName); jsonVideos.put("address",filename); fileNs.add(jsonVideos); } } } String pageNum = json.get("pageNum"); String pageSize = json.get("pageSize"); return PageUtil.startPage(fileNs, Integer.parseInt(pageNum), Integer.parseInt(pageSize)); }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3